package com.ld.security.demo.base.wrapper;

import com.ld.security.auth.shiro.util.LoginUtil;
import com.ld.security.common.core.base.QueryPo;
import com.ld.security.common.core.base.SuperPo;
import com.ld.security.common.mybatis.core.entity.SqlEntity;
import com.ld.security.common.mybatis.core.entity.SqlUserEntity;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.common.mybatis.wrapper.SimpWrapperFactory;
import com.ld.security.server.admin.modules.ge.sql.SqlProjectPo;
import com.ld.security.server.admin.modules.ge.util.ProjectUtil;
import org.springframework.stereotype.Component;

@Component
public class AuthWrapper extends SimpWrapperFactory {


    @Override
    public <V> EntityWrapper<V> getWrapper(SuperPo po) {


        SuperPo q=po;


        if(po instanceof QueryPo){
            QueryPo<SuperPo> queryPo = (QueryPo)po;
            q=queryPo.getQuery();
        }


        if(q instanceof SqlEntity){


             if(q instanceof SqlUserEntity){
                 SqlUserEntity sqlUserEntity = (SqlUserEntity)q;
                 sqlUserEntity.setCrtUser(LoginUtil.getUserId());

             }

                if(q instanceof SqlProjectPo){
                    SqlProjectPo sqlProjectPo= (SqlProjectPo)q;
                    String projectId =  ProjectUtil.getDefProectId();
                    sqlProjectPo.setProjectId(projectId);
                }
        }


        EntityWrapper ew ;

        if(q != null){
           ew =  EntityWrapper.of(q);


        }else{
            ew =  EntityWrapper.of();
        }



        return ew;


    }
}
